﻿using System;
using System.Globalization;
using System.IO;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class OtherController : BaseController
    {
         public virtual IAdminLogService<SiteLog> logService { get; set; }

         public OtherController()
        {
            HidePermission(typeof(wojilu.Web.Controller.Admin.SecurityController));
            logService = new SiteLogService();
        }

         [HttpGet, HttpPost]
         public virtual void FileUp()
         {
             var ybb_admininfo = ctx.Get("ybb_admininfo");
             var ybb_powerlist = ctx.Get("ybb_powerlist");
             if (ybb_admininfo.StrIsNullOrEmpty() || ybb_powerlist.StrIsNullOrEmpty())
             {
                 echoRedirect(lang("exPlsLogin"), t2(new AdminController().Login) + "?returnUrl=" + ctx.url.EncodeUrl);
                 return;
             }
             if (ctx.HasUploadFiles)
             {
                 var oFile = ctx.GetFileSingle();//得到要上传文件
                 if (oFile != null && oFile.ContentLength > 0)
                 {
                     var filename = Path.GetFileName(oFile.FileName);
                     var fileExtension = Path.GetExtension(filename).ToLower();//上传文件的扩展名
                     var filetype1 = oFile.ContentType.ToLower();
                     if (!StringArray.InArray(fileExtension.TrimStart('.'), configsService.GetValue("upfile_pictype"), "|") && !filetype1.StartsWith("image"))
                     {
                         ctx.web.ResponseWrite("不安全的图片格式，换一张吧。");
                     }
                     try
                     {
                         if (!StringArray.InArray(fileExtension.TrimStart('.'), configsService.GetValue("upfile_pictype"), "|") && !filetype1.StartsWith("image"))
                         {
                             ctx.web.ResponseWrite("您上传的文件格式不受系统支持！");
                             ctx.web.ResponseEnd();
                         }
                         else
                         {
                             var filesize = oFile.ContentLength;
                             if (filesize > TypeCon.StrToInt(configsService.GetValue("upfile_maxsize")))
                             {
                                 ctx.web.ResponseWrite("文件大小超过限制！");
                                 ctx.web.ResponseEnd();
                             }
                             else
                             {
                                 var FilePath = configsService.GetValue("upfile_dir") + DateTime.Now.ToString("yyyy/MM/dd/", DateTimeFormatInfo.InvariantInfo);
                                 var FileName = (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + "_" + Rand.MakeRandomString("0123456789", 3));
                                 if (!FileSystemObject.IsExist(PathHelper.Map(sys.Path.Root + FilePath), FsoMethod.Folder))
                                 {
                                     FileSystemObject.Create(PathHelper.Map(sys.Path.Root + FilePath), FsoMethod.Folder);
                                 }
                                 var OFilePath = FilePath + FileName + fileExtension;
                                 oFile.SaveAs(PathHelper.Map(sys.Path.Root + OFilePath));
                                 var tid = ctx.GetInt("tid");
                                 var mid = ctx.GetInt("mid");
                                 var modeltype = TypeList.findById(tid);
                                 var model = new Document();
                                 model.lng = ctx.Get("lng");
                                 model.pid = 50;
                                 model.mid = mid;
                                 model.aid = 1;
                                 model.tid = tid;
                                 model.sid = 0;
                                 model.fgid = 0;
                                 model.isclass = 1;
                                 model.islink = 0;
                                 model.ishtml = 1;
                                 model.ismess = 1;
                                 model.isorder = 1;
                                 model.ktid = 0;
                                 model.purview = 0;
                                 model.istemplates = 0;
                                 model.isbase = 0;
                                 model.tsn = "SN" + (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + Rand.MakeRandomString("0123456789", 3));
                                 model.title = modeltype.typename;
                                 model.pic = sys.Path.Root + OFilePath;
                                 model.click = 0;
                                 model.addtime = DateTime.Now;
                                 model.uptime = DateTime.Now;
                                 model.template = "article_read";
                                 model.filename = "0";
                                 model.filepath = modeltype.dirpath;
                                 model.filepage = 1;
                                 model.insert();
                                 var did = model.Id;
                                 var da = new Document_Attr();
                                 da.did = did;
                                 da.insert();
                                 var dc = new Document_Content();
                                 dc.did = did;
                                 dc.insert();
                                 ctx.web.ResponseWrite("ok");
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         ctx.web.ResponseWrite("程序异常，上传未成功。");
                     }
                 }
                 else
                 {
                     ctx.web.ResponseWrite("请选择上传文件。");
                 }
             }
             else
             {
                 ctx.web.ResponseWrite("上传有误。");
             }
             ctx.web.ResponseEnd();
         }

    }
}
